package cn.org.xwb.bookmall.user.controller;

import cn.org.xwb.bookmall.model.constant.AccountConst;
import cn.org.xwb.bookmall.user.annotation.IsLoginParam;
import cn.org.xwb.bookmall.user.service.XwbUserService;
import cn.org.xwb.common.encrypt.annotation.des.DesEn;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDe;
import cn.org.xwb.common.encrypt.annotation.rsa.RsaDeParam;
import cn.org.xwb.common.encrypt.constant.EncryptConstant;
import cn.org.xwb.common.model.vo.RespResult;
import cn.org.xwb.common.valid.annotation.nu.NotNull;
import cn.org.xwb.common.valid.annotation.nu.NotNullParam;
import cn.org.xwb.common.valid.annotation.str.StrNotNull;
import cn.org.xwb.common.valid.annotation.str.StrNotNullParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class XwbUserController {

    @Autowired
    private XwbUserService xwbUserService;

    @GetMapping("/all")
    @StrNotNull
    @RsaDe
    @IsLoginParam
    @DesEn
    public RespResult<Object> getUserAllInfo(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey
    ) {
        return xwbUserService.getUserAllInfo(account);
    }

    @PostMapping("/avatar")
    @NotNull
    @StrNotNull
    @RsaDe
    @IsLoginParam
    @DesEn
    public RespResult<Object> updateAvatar(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey,
            @RequestParam("avatar")
            @StrNotNullParam @RsaDeParam
            String avatar,
            @RequestParam("id")
            @NotNullParam
            Long id
    ) {
        return xwbUserService.updateAvatar(avatar, id);
    }

    @PostMapping("/avatar/bg")
    @NotNull
    @StrNotNull
    @RsaDe
    @IsLoginParam
    @DesEn
    public RespResult<Object> updateAvatarBg(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey,
            @RequestParam("avatarBg")
            @StrNotNullParam @RsaDeParam
            String avatarBg,
            @RequestParam("id")
            @NotNullParam
            Long id
    ) {
        return xwbUserService.updateAvatarBg(avatarBg, id);
    }

    @PostMapping("/username")
    @NotNull
    @StrNotNull
    @RsaDe
    @IsLoginParam
    @DesEn
    public RespResult<Object> updateUsername(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam(EncryptConstant.DES_KEY_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String desKey,
            @RequestParam("username")
            @StrNotNullParam @RsaDeParam
            String username,
            @RequestParam("id")
            @NotNullParam
            Long id
    ) {
        return xwbUserService.updateUsername(username, id);
    }

    @GetMapping("/mail/code/pwd")
    @StrNotNull
    @RsaDe
    @IsLoginParam
    public RespResult<Object> getUpdatePwdMailCode(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam("mail")
            @StrNotNullParam @RsaDeParam
            String mail
    ) {
        return xwbUserService.getUpdatePwdMailCode(mail, account);
    }

    @PostMapping("/pwd")
    @NotNull
    @StrNotNull
    @RsaDe
    @IsLoginParam
    public RespResult<Object> updatePwd(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam("code")
            @StrNotNullParam @RsaDeParam
            String code,
            @RequestParam("pwd")
            @StrNotNullParam @RsaDeParam
            String pwd,
            @RequestParam("id")
            @NotNullParam
            Long id
    ) {
        return xwbUserService.updatePwd(code, account, pwd, id);
    }

    @PostMapping("/info")
    @NotNull
    @StrNotNull
    @RsaDe
    @IsLoginParam
    public RespResult<Object> updateUserInfo(
            @RequestParam(AccountConst.ACCOUNT_PARAM_NAME)
            @StrNotNullParam @RsaDeParam
            String account,
            @RequestParam("phone") @StrNotNullParam @RsaDeParam
            String phone,
            @RequestParam("gender") @StrNotNullParam @RsaDeParam
            String gender,
            @RequestParam("id") @NotNullParam
            Long id
    ) {
        return xwbUserService.updateUserInfo(phone, gender, id);
    }
}
